const UserService = require("../../services/admin/UserService")
const JWT = require("../../util/JWT")
const svgCaptcha = require('svg-captcha'); //验证码模块

const UserController = {
    // 拿到前端的数据进行分析拆解 拿回来送给service进行处理 再返回
    login: async(req,res)=>{ 
        const captcha = svgCaptcha.create();

        var result = await UserService.login(req.body)
        if(result.length===0){
            res.send({
                code:"-1",
                error:"用户名密码不匹配",
                captchaData:{
                    text:captcha.text,
                    data:captcha.data,
                }
            })
        }else{
            //生成token
            const token = JWT.generate({
                _id:result[0]._id,
                username:result[0].username
            },"1d") // 设置时长 app.js里面有 "10s"   1d = 1 天
            res.header("Authorization",token)
            res.send({
                ActionType:"OK",
                data:{
                    username:result[0].username,
                    gender:result[0].gender?result[0].gender:0,//性别 0,1,2
                    introduction:result[0].introduction,//简介
                    avatar:result[0].avatar, //头像
                    role:result[0].role//管理员1，编辑2
                },
                captchaData:{
                    text:captcha.text,
                    data:captcha.data,
                }
            })
        }
    },

    upload:async(req,res)=>{
        const {username,introduction,gender} = req.body
        //调用servise模块更新数据
        const token = req.headers["authorization"].split(" ")[1]
        const avatar = req.file?`/avataruploads/${req.file.filename}`:""
        var payload = JWT.verify(token)

        await UserService.upload({_id:payload._id,username,introduction,
        gender:Number(gender),avatar})
        if(avatar){
            res.send({
                ActionType:"ok",
                data:{
                    username,introduction,
                    gender:Number(gender),
                    avatar
                }
            })
        }else{
            res.send({
                ActionType:"ok",
                data:{
                    username,introduction,
                    gender:Number(gender)
                }
            })
        }
    },

    add:async(req,res)=>{
        const {username,introduction,gender,role,password} = req.body
        //调用servise模块更新数据
        // const token = req.headers["authorization"].split(" ")[1]
        const avatar = req.file?`/avataruploads/${req.file.filename}`:""
        // var payload = JWT.verify(token)

        await UserService.add({username,introduction,
        gender:Number(gender),avatar,role:Number(role),password})
        if(avatar){
            res.send({
                ActionType:"ok",
                
            })
        }
    },

    getList:async(req,res)=>{
        const result = await UserService.getList(req.params)
        // console.log(result)
        res.send({
            ActionType:"ok",
            data:result
        })
    }, 
    putList:async(req,res)=>{
        const result = await UserService.putList(req.body)
        // console.log(result)
        res.send({
            ActionType:"ok",
        })
    },   
    delList:async(req,res)=>{
        // console.log(req.params.id)
        const result = await UserService.delList({_id:req.params.id})
        res.send({
            ActionType:"delok",
        })
    },
    

}
module.exports = UserController
